// pages/audit-expert/audit-expert.js
Page({
  data: {
    isShow:true,//是否显示左侧按钮
    backgroundColor:'white',//背景颜色
    navTitle:'专家申请',//标题
    isWhite:true,//是否白色胶囊
    titleColor:'black',//字体颜色
    user_name:'',
    name:"",
    place_work:"",
    address:"",
    introduce:"",
    // titles: [{ title: '工程师' }, { title: '技术员' }],
    // selectedTitle:"",
    experienceDurations: [{ duration: '1-3年' }, { duration: '3-5年' }, { duration: '5-10年' }, { duration: '10年以上' }],
    selectedDureations:"",
    crops: [{crop:'苹果'}, {crop:'番茄'}, {crop:'草莓'}, {crop:'桃子'},{crop:'杏'}],
    selectCrops:"",
    imageSrc: ''
  },
  return(){
    wx.switchTab({
      url: '../logs/logs',
    })
  },
  inputH1(e){
    this.setData({
      name:e.detail.value
    })
  },
  inputH2(e){
    this.setData({
      place_work:e.detail.value
    })
  },  
  inputH3(e){
    this.setData({
      address:e.detail.value
    })
  },  
  inputH4(e){
    this.setData({
      introduce:e.detail.value
    })
  },
  // bindPickerChange1: function (e) {
  //   this.setData({
  //     selectedTitle: this.data.titles[e.detail.value].title
  //   });
  //   console.log(this.data.selectedTitle)
  // },
  bindPickerChange2: function (e) {
    this.setData({
      selectedDureations: this.data.experienceDurations[e.detail.value].duration
    });
    console.log(this.data.selectedDureations)
  },

  bindPickerChange3: function (e) {
    this.setData({
      selectedCrops: this.data.crops[e.detail.value].crop
    });
    console.log(this.data.selectedCrops)
  },
  
  uploadImage: function() {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: function(res) {
        that.setData({
          imageSrc: res.tempFilePaths[0]
        });
      }
    });
  },
submitForm(){
    console.log("dianji")
    console.log(this.data.name)
    console.log(this.data.place_work)
    console.log(this.data.address)
    console.log(this.data.selectedTitle)
    console.log(this.data.selectedDureations)
    console.log(this.data.selectedCrops)
    console.log(this.data.introduce)
    console.log(this.data.imageSrc)

    if (!this.data.name || !this.data.place_work || !this.data.address ||  !this.data.selectedDureations || !this.data.selectedCrops || !this.data.introduce || !this.data.imageSrc) {
      wx.showToast({
        icon: 'none',
        title: '请填写所有必填项',
        duration: 2000
      });}
      else {
        // 提交表单数据到服务器的代码
        console.log('所有必填项已填写');
        wx.cloud.database().collection("expert")
        .add({
          data:{
            user_name:this.data.user_name,
            name:this.data.name,
            place_work:this.data.place_work,
            address:this.data.address,
            experience:this.data.selectedDureations,
            crops:this.data.selectedCrops,
            introduce:this.data.introduce,
            imageSrc:this.data.imageSrc
          }
        })
        .then(res=>{
          console.log("添加成功")
          wx.showToast({
            icon: 'none',
            title: '提交成功',
            duration: 2000
          });
        })
        .catch(err=>{
          console.log("添加失败")
        })
      }
},
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.init()
  },
  init(){
    // 获取当前登录用户名
    const user = wx.getStorageSync('user2');
    console.log('用户名',user.name)
    this.setData({
      user_name:user.name
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})